#include <fstream>  
#include <iostream>  
#include <string>  
#include <streambuf>  
using namespace std;  

/*

https://www.cnblogs.com/chechen/p/7479809.html
https://blog.csdn.net/u013571243/article/details/49250213

*/


void test_case1() {

   const int len=20;  
   char str[len];  
   ifstream OpenFile("file.txt");  
   if (OpenFile.fail())  
    {  
        cout<<"打开文件错误!"<<endl;  
        exit(0);  
    }  
    OpenFile>>str;   
    cout<<str<<endl;  
    OpenFile.close(); 

}

void test_case2_read_whole_file() {

  
    std::ifstream t("file.txt");  
    std::string str((std::istreambuf_iterator<char>(t)),  
                 std::istreambuf_iterator<char>());
    std::cout << "file content: " << str << std::endl;

}

#include <sstream>  

void test_case3_read_whole_file() {

    std::ifstream t("file.txt");  
    std::stringstream buffer;  
    buffer << t.rdbuf();  
    std::string contents(buffer.str());
    std::cout << "file content: " << contents << std::endl;

}


int main(void)  {  
  
    test_case3_read_whole_file();
    return 0;
}  